package com.elean.pms.service.impl;

import com.elean.pms.utils.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * @Auther: 周作为
 * @Date: 2020/3/2 17:01
 * @Description:
 */
public class Consumer implements Runnable {


    private JedisPool jedisPool;

    String redisKey;

    public Consumer(JedisPool jedisPool, String redisKey) {
        this.jedisPool = jedisPool;
        this.redisKey = redisKey;
    }

    @Override
    public void run() {
        while (true) {
            Jedis jedis =jedisPool.getResource();
            try {
                //阻塞队列，1小时没有数据释放连接
                List<String> result = jedis.blpop(60 * 60 * 1, redisKey);
                if (result != null && result.size() > 1) {
                    String key = result.get(0);
                    String value = result.get(1);
                    System.out.println("value==="+value);
                }
            } catch (Exception e) {
               e.printStackTrace();
            } finally {
                jedis.close();
            }

        }
    }
}
